package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"guanli/models"
)

type LoginController struct {
	beego.Controller
}
type Msg struct {
	Code int
	Msg  string
	Data interface{}
}

func (c *LoginController) Register() {
	username := c.GetString("username")
	password := c.GetString("password")
	login := models.Login{
		Username: username,
		Password: password,
	}

	if username == "" || password == "" {
		res := Msg{
			Code: 0,
			Msg:  "账号密码不能为空",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	login.Username = username
	if err := login.GetByUsername(); err != nil {
		res := Msg{
			Code: 0,
			Msg:  "系统错误",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	if login.ID != 0 {
		res := Msg{
			Code: 0,
			Msg:  "账号已存在",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	if err := login.Created(); err != nil {
		res := Msg{
			Code: 0,
			Msg:  "注册失败",
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}
	res := Msg{
		Code: 200,
		Msg:  "注册成功",
	}
	c.Data["json"] = res
	c.ServeJSON()

}

//func (c *LoginController) Login() {
//	username := c.GetString("username")
//	password := c.GetString("password")
//	login := models.Login{
//		Username: username,
//		Password: password,
//	}
//	if login.Username != username {
//		res := Msg{
//			Code: 0,
//			Msg:  "用户不存在",
//		}
//		c.Data["json"] = res
//		c.ServeJSON()
//	}
//}
